package com.hanlink.dsp.model;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C): 2015/10/15
 *
 * Description:
 *
 * Revision History
 * Date			  Author		Description
 * 2015/10/15        PeterVon       初始创建
 *
 * </pre>
 */
public class ClassicDevice implements Parcelable {

    public ConnectState getState() {
        return state;
    }

    public void setState(ConnectState state) {
        this.state = state;
    }

    public enum ConnectState {
        STATE_NORMAL(0), STATE_PAIRED(1), STATE_CONNECT_SUCCESS(2), STATE_CONNECT_FAIL(3), STATE_CONNECTING(4);

        int state;

        ConnectState(int state) {
            this.state = state;
        }

        public int getState() {
            return state;
        }

        public static ConnectState parseState(int state) {
            switch (state) {
                case 0:
                    return STATE_NORMAL;
                case 1:
                    return STATE_PAIRED;
                case 2:
                    return STATE_CONNECT_SUCCESS;
                case 3:
                    return STATE_CONNECT_FAIL;
                case 4:
                    return STATE_CONNECTING;
            }
            return null;
        }
    }

    //0 正常扫描 , 1 配对成功 2 正在连接 3 连接成功　４连接失败
    private ConnectState state = ConnectState.STATE_NORMAL;


    /**
     * 蓝牙设备
     */
    private BluetoothDevice bluetoothDevice;

    /**
     * 蓝牙Socket
     */
    private BluetoothSocket mBluetoothSocket;

    public ClassicDevice(BluetoothDevice device) {
        bluetoothDevice = device;
    }

    protected ClassicDevice(Parcel in) {
        state = ConnectState.parseState(in.readInt());
        bluetoothDevice = in.readParcelable(BluetoothDevice.class.getClassLoader());
    }

    public static final Creator<ClassicDevice> CREATOR = new Creator<ClassicDevice>() {
        @Override
        public ClassicDevice createFromParcel(Parcel in) {
            return new ClassicDevice(in);
        }

        @Override
        public ClassicDevice[] newArray(int size) {
            return new ClassicDevice[size];
        }
    };

    public BluetoothDevice getBluetoothDevice() {
        return bluetoothDevice;
    }


    public boolean isConnected() {
        return state == ConnectState.STATE_CONNECT_SUCCESS;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(state.getState());
        dest.writeParcelable(bluetoothDevice, flags);
    }

    public BluetoothSocket getBluetoothSocket() {
        return mBluetoothSocket;
    }

    public void setBluetoothSocket(BluetoothSocket mBluetoothSocket) {
        this.mBluetoothSocket = mBluetoothSocket;
    }


}
